<?php
/**
 * Class that operate on table 'admin_users'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2015-05-26 14:36
 */
class AdminUsersMySqlDAO implements AdminUsersDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return AdminUsersMySql 
	 */
	public function load($adminusers){
		$sql = 'SELECT admin_users.*,roles.role_id,roles.first_name AS role_name FROM admin_users, roles WHERE roles.role_id=admin_users.role_id and admin_id = "'.$adminusers->admin_id.'"';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->getRow($sqlQuery);
	}
	
	public function load_roles(){
		$sql = 'SELECT role_id,first_name FROM roles';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	public function login($adminusers){
		$sql = 'SELECT * FROM admin_users WHERE user_id = "'.$adminusers->user_id.'" and password = "'.$adminusers->password.'"';
		$sqlQuery = new SqlQuery($sql);
		return $this->getRow($sqlQuery);
	}
	/**
	 * Get all records from table
	 */
	 
	public function queryAll($adminusers){
		$sql = 'SELECT * FROM admin_users order by '.$adminusers->order_by.' '.$adminusers->sort_order.' limit '.$adminusers->results_per_page.' offset '.$adminusers->offset.'';
		
		/* $sql = 'SELECT * FROM admin_users where '.$adminusers->search_by.' like "%'.$adminusers->search_by_value.'%"  order by '.$adminusers->order_by.' '.$adminusers->sort_order.' limit '.$adminusers->results_per_page.' offset '.$adminusers->offset.''; */
		//	print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllSearchBy($adminusers){
		$sql = 'SELECT * FROM admin_users WHERE '.$adminusers->search_by.' like "%'.$adminusers->search_by_value.'%" order by '.$adminusers->order_by.' '.$adminusers->sort_order.' limit '.$adminusers->results_per_page.' offset '.$adminusers->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	public function searchby_count($adminusers){
		$sql = 'SELECT count(*) FROM admin_users WHERE '.$adminusers->search_by.' like "%'.$adminusers->search_by_value.'%"';
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	public function adminuser_count($adminusers){
		$sql = 'SELECT count(*) FROM admin_users order by '.$adminusers->order_by.' '.$adminusers->sort_order.' limit '.$adminusers->results_per_page.' offset '.$adminusers->offset.'';
		$sqlQuery = new SqlQuery($sql);
		return $this->querySingleResult($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param adminUser primary key
 	 */
	public function delete($admin_id){
		$sql = 'DELETE FROM admin_users WHERE admin_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($admin_id);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param AdminUsersMySql adminUser
 	 */
	public function insert($adminUser){
		$sql = 'INSERT INTO admin_users (first_name, middle_name, last_name, email, user_id, password, role_id, created_date, updated_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($adminUser->first_name);
		$sqlQuery->set($adminUser->middle_name);
		$sqlQuery->set($adminUser->last_name);
		$sqlQuery->set($adminUser->email);
		$sqlQuery->set($adminUser->user_id);
		$sqlQuery->set($adminUser->password);
		$sqlQuery->setNumber($adminUser->role_id);
		$sqlQuery->set($adminUser->created_date);
		$sqlQuery->set($adminUser->updated_date);

		$id = $this->executeInsert($sqlQuery);	
		$adminUser->admin_id = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param AdminUsersMySql adminUser
 	 */
	public function update($adminusers){
		$sql = 'UPDATE admin_users SET first_name = "'.$adminusers->first_name.'", middle_name = "'.$adminusers->middle_name.'", last_name = "'.$adminusers->last_name.'", email = "'.$adminusers->email.'", user_id = "'.$adminusers->user_id.'", password = "'.$adminusers->password.'", role_id = "'.$adminusers->role_id.'", status = "'.$adminusers->status.'", updated_date = "'.$adminusers->updated_date.'" WHERE admin_id = "'.$adminusers->admin_id.'"';
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}
	public function delete_adminuser($adminusers){
		//print_r("hello");exit;
		$sql = 'UPDATE admin_users SET status = "'.$adminusers->status.'" WHERE admin_id = "'.$adminusers->admin_id.'"'; 
		//print_r($sql);exit;
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM admin_users';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByFirstName($value){
		$sql = 'SELECT * FROM admin_users WHERE first_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByMiddleName($value){
		$sql = 'SELECT * FROM admin_users WHERE middle_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByLastName($value){
		$sql = 'SELECT * FROM admin_users WHERE last_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByEmail($value){
		$sql = 'SELECT * FROM admin_users WHERE email = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUserId($value){
		$sql = 'SELECT * FROM admin_users WHERE user_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByPassword($value){
		$sql = 'SELECT * FROM admin_users WHERE password = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByRoleId($value){
		$sql = 'SELECT * FROM admin_users WHERE role_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByStatus($value){
		$sql = 'SELECT * FROM admin_users WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCreatedDate($value){
		$sql = 'SELECT * FROM admin_users WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdatedDate($value){
		$sql = 'SELECT * FROM admin_users WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByFirstName($value){
		$sql = 'DELETE FROM admin_users WHERE first_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByMiddleName($value){
		$sql = 'DELETE FROM admin_users WHERE middle_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByLastName($value){
		$sql = 'DELETE FROM admin_users WHERE last_name = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByEmail($value){
		$sql = 'DELETE FROM admin_users WHERE email = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUserId($value){
		$sql = 'DELETE FROM admin_users WHERE user_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByPassword($value){
		$sql = 'DELETE FROM admin_users WHERE password = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByRoleId($value){
		$sql = 'DELETE FROM admin_users WHERE role_id = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByStatus($value){
		$sql = 'DELETE FROM admin_users WHERE status = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCreatedDate($value){
		$sql = 'DELETE FROM admin_users WHERE created_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdatedDate($value){
		$sql = 'DELETE FROM admin_users WHERE updated_date = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return AdminUsersMySql 
	 */
	protected function readRow($row){
		$adminUser = new AdminUser();
		
		$adminUser->admin_id = $row['admin_id'];
		$adminUser->first_name = $row['first_name'];
		$adminUser->middle_name = $row['middle_name'];
		$adminUser->last_name = $row['last_name'];
		$adminUser->email = $row['email'];
		$adminUser->user_id = $row['user_id'];
		$adminUser->password = $row['password'];
		$adminUser->role_id = $row['role_id'];
		$adminUser->role_name = $row['role_name'];
		$adminUser->status = $row['status'];
		$adminUser->created_date = $row['created_date'];
		$adminUser->updated_date = $row['updated_date'];

		return $adminUser;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return AdminUsersMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>